//邮件格式的验证
export function isEmail(val) {
  let bean = {
    success: true,
    msg: ''
  }
  let regx = new RegExp(
    "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
    'i'
  )
  if (val != null && val != '' && val.length > 0) {
    if (!regx.test(val)) {
      bean.success = false
    }
  }
  if (!bean.success) {
    bean.msg = '邮件格式不正确'
  }
  return bean
}

//只允许数字和英文字符
export const NumberAndCodeREG = (value) => {
  let regex = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$/
  return regex.test(value)
}

// 验证是否是汉字
export function isChineseValidate(s) {
  const strcheck = /^[\u4E00-\u9FA5]{1,}$/g
  const bean = {
    success: false
  }
  if (strcheck.test(s)) {
    bean.success = true
  } else {
    bean.success = false
    bean.msg = '请输入汉字'
  }
  return bean
}

/**
 * 手机号验证函数
 * @function mobileValidate
 * @param {string} val - 将要验证的字符串.
 * @returns {object} {success:true or false,msg:'验证信息'}
 * @example
 * let phoneNum = "13840056069";
 * let vo = VerifyUtil.mobileValidate(phoneNum);
 * //console.log(vo) 输出为{success:true,msg:''}
 */
export function mobileValidate(val) {
  let obj = {
    success: true,
    msg: ''
  }
  //由于国际化需要，暂时关闭电话号码验证功能
  //xujingxue
  // let regx = /(^0{0,1}(13|14|15|17|18|19)[0-9]{9}$)/

  // 生产手机号规则修改
  let regx = /(^[0|1]{1}[0-9]{10}$)/
  if (val != null && val != '' && val.length > 0) {
    if (!regx.test(val)) {
      obj.success = false
    }
  }
  if (!obj.success) {
    obj.msg = '手机号码格式不正确，正确格式如：13888888888'
  }
  return obj
}

/**
 * 座机or传真验证函数
 * @function isTelephoneNumber
 * @param {string} val - 将要验证的字符串.
 * @returns {object} {success:true or false,msg:'验证信息'}
 * @example
 * let phoneNum = "024-22252252";
 * let vo = VerifyUtil.isTelephoneNumber(phoneNum);
 * //console.log(vo) 输出为{success:true,msg:''}
 */
export function isTelephoneNumber(val) {
  let obj = {
    success: true,
    msg: ''
  }
  //由于国际化需要，暂时关闭电话号码验证功能
  //xujingxue
  let regx = /((^0\d{2,3})-?)?\d{7,8}$/
  if (val != null && val != '' && val.length > 0) {
    if (!regx.test(val)) {
      obj.success = false
    }
  }
  let errorMsg = ''
  errorMsg += '座机或传真格式不正确，正确格式：024-88888888'
  if (!obj.success) {
    obj.msg = errorMsg
  }
  return obj
}

/**
 * 电话号码，包含座机和手机
 * @function isMobileOrTelephoneNumber
 * @param {string} val - 将要验证的字符串.
 * @returns {object} {success:true or false,msg:'验证信息'}
 * @example
 * let phoneNum = "024-22252252";
 * let vo = VerifyUtil.isTelephoneNumber(phoneNum);
 * //console.log(vo) 输出为{success:true,msg:''}
 */
export function isMobileOrTelephoneNumber(val) {
  let obj = {
    success: true,
    msg: ''
  }
  let r1 = mobileValidate(val)
  let r2 = isTelephoneNumber(val)
  if (r1.success === false && r2.success === false) {
    obj.success = false
  }

  let errorMsg = ''
  errorMsg += '联系方式输入不正确，正确格式：13888888888，024-88888888'
  if (!obj.success) {
    obj.msg = errorMsg
  }
  return obj
}

// 身份证校验规则
export function personIdNumber(value) {
  let obj = {
    success: true,
    msg: ''
  }
  if (value.length == 18) {
    let re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
    let arrSplit = value.match(re) //检查生日日期是否正确，value就是身份证号
    let dtmBirth = new Date(arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
    let bGoodDay =
      dtmBirth.getFullYear() == Number(arrSplit[2]) &&
      dtmBirth.getMonth() + 1 == Number(arrSplit[3]) &&
      dtmBirth.getDate() == Number(arrSplit[4])
    if (!bGoodDay) {
      obj.success = false
      obj.msg = '出生日期不对'
    } else {
      //检验18位身份证的校验码是否正确。
      //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
      let valnum
      let arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
      let arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
      let nTemp = 0,
        i
      for (i = 0; i < 17; i++) {
        nTemp += value.substr(i, 1) * arrInt[i]
      }
      valnum = arrCh[nTemp % 11]
      if (valnum != value.substr(17, 1)) {
        obj.success = false
        obj.msg = '18位身份证的校验码不正确'
      }
      return true
    }
  } else {
    obj.success = false
    obj.msg = '身份证长度不准确'
  }
  return obj
}
